package com.balance.anole.erp.security.login.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.balance.anole.erp.security.login.model.RetailLoginUser;

public class LoginDelegator {

	private static Map<LoginType, ILoginHandler> loginHandlers = new HashMap<LoginType, ILoginHandler>();

	private static LoginDelegator instance = new LoginDelegator();

	private LoginDelegator(){
		//initialize login type and corresponding handlers.
/*		LoginType shop = new LoginType(Messages.LOGIN_DIALOG_LOGIN_TYPE_SHOP);
		loginHandlers.put(shop, new LocalLoginHandler());
		shop = new LoginType(Messages.LOGIN_DIALOG_LOGIN_TYPE_AGENT);
		loginHandlers.put(shop, new RemoteLoginHandler());*/
	}

	public List<LoginType> getLoginTypes(){
		return new ArrayList<LoginType>(loginHandlers.keySet());
	}

	public static LoginDelegator getInstance(){
		return instance;
	}

	public void registerLoginHandler(LoginType loginType, ILoginHandler handler){
		loginHandlers.put(loginType, handler);
	}

	public RetailLoginUser login(LoginContext context){
		ILoginHandler handler = loginHandlers.get(context.getLoginType());
		if(handler == null){
			throw new RuntimeException("No login handler is specified for type " + context.getLoginType()); //$NON-NLS-1$
		}
		return handler.login(context.getUser());
	}
}
